package com.school.vaccination.controller;

import com.github.pagehelper.PageInfo;
import com.school.support.base.BaseController;
import com.school.vaccination.domain.Vaccine;
import com.school.vaccination.service.VaccineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import zhibi.common.support.base.Page;
import zhibi.common.support.exception.MessageException;
import zhibi.common.support.mybatis.example.Example;
import zhibi.common.support.mybatis.example.ExampleType;
import zhibi.common.support.mybatis.example.ExampleUtil;

import java.util.Date;
import java.util.Objects;

/**
 * Created by 执笔 on 2017/3/18.
 */
@Controller
@RequestMapping("vaccine")
public class VaccineController extends BaseController {

    @Autowired
    private VaccineService vaccineService;

    /**
     * 疫苗列表
     *
     * @param model
     * @param vaccine
     * @param page
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, Vaccine vaccine, Page page) {
        Example example = ExampleUtil.getInstance()
                .addParam("name", vaccine.getName(), ExampleType.Operation.LIKE)
                .addParam("number", vaccine.getNumber(), ExampleType.Operation.LIKE)
                .addOrder("addtime", ExampleType.OrderType.DESC)
                .getExample();
        PageInfo<Vaccine> pageInfo = vaccineService.selectByExample(example, page);
        setModelAttribute(model, pageInfo);
        return "vaccine/list";
    }

    /**
     * 跳转到添加疫苗页面
     *
     * @return
     */
    @RequestMapping("add")
    public String add() {
        return "vaccine/add";
    }

    /**
     * 添加疫苗
     *
     * @param vaccine
     * @return
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(Vaccine vaccine) {
        Vaccine vaccineTemp = new Vaccine();
        vaccineTemp.setNumber(vaccine.getNumber());
        if (null != vaccineService.selectOne(vaccineTemp)) {
            throw new MessageException("该疫苗编号已经存在");
        }
        if(vaccine.getProducttime().compareTo(vaccine.getValidtime()) >0){
            throw new MessageException("生产日期不能大于有效日期");
        }
        vaccine.setAddtime(new Date());
        vaccineService.insertSelective(vaccine);
        return redirect("list" );
    }

    /**
     * 疫苗详情
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Vaccine vaccine = vaccineService.selectByPK(id);
        if (null == vaccine) {
            throw new MessageException("该疫苗不存在");
        }
        model.addAttribute(vaccine);
        return "vaccine/detail";
    }

    /**
     * 更新疫苗信息
     *
     * @param vaccine
     * @return
     */
    @RequestMapping("update")
    public String update(Vaccine vaccine) {
        Vaccine vaccineTemp = new Vaccine();
        vaccineTemp.setNumber(vaccine.getNumber());
        vaccineTemp = vaccineService.selectOne(vaccineTemp);
        if (null != vaccineTemp && !Objects.equals(vaccine.getId(), vaccineTemp.getId())) {
            throw new MessageException("该疫苗编号已经存在");
        }
        if(vaccine.getProducttime().compareTo(vaccine.getValidtime()) >0){
            throw new MessageException("生产日期不能大于有效日期");
        }
        vaccineService.updateByIdSelective(vaccine);
        return redirect("list" );
    }

    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id){
        Vaccine vaccine = vaccineService.selectByPK(id);
        if (null == vaccine) {
            throw new MessageException("该疫苗不存在");
        }
        if(vaccine.getStatus() != STATE_2){
            throw new MessageException("该疫苗不能销毁");
        }
        vaccine.setStatus(STATE_3);
        vaccineService.updateByIdSelective(vaccine);
        return refresh();
    }

    @RequestMapping("overlist")
    public String overlist(Model model, Vaccine vaccine, Page page){
        Example example = ExampleUtil.getInstance()
                .addParam("name", vaccine.getName(), ExampleType.Operation.LIKE)
                .addParam("number", vaccine.getNumber(), ExampleType.Operation.LIKE)
                .addOrder("addtime", ExampleType.OrderType.DESC)
                .addParam("status",STATE_2)
                .getExample();
        PageInfo<Vaccine> pageInfo = vaccineService.selectByExample(example, page);
        setModelAttribute(model, pageInfo);
        return "vaccine/overlist";
    }

}
